package com.exam.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exam.dto.ScoreSearchParam;
import com.exam.entity.Score;
import com.exam.mapper.ScoreMapper;
import com.exam.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;
    @Override
    public int add(Score score) {
        return scoreMapper.add(score);
    }

    @Override
    public List<Score> findAll() {
        return scoreMapper.findAll();
    }

    @Override
    public IPage<Score> findById(Page<Score> page, Long studentId) {
        return scoreMapper.selectPage(page,new LambdaQueryWrapper<Score>().eq(Score::getStudentId,studentId));
    }

    @Override
    public List<Score> findById(Long studentId) {
        return scoreMapper.findById(studentId);
    }

    @Override
    public List<Score> findByExamCode(Integer examCode) {
        return scoreMapper.findByExamCode(examCode);
    }

    public IPage<Score> searchByKeyWord(Page<Score> page,ScoreSearchParam searchParam) {
        LambdaQueryWrapper<Score> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Score::getStudentId,searchParam.getStudentId());
        if (!StringUtils.isEmpty(searchParam.getMajor())){
            queryWrapper.eq(Score::getSubject,searchParam.getMajor());
        }
        if (searchParam.getSix() == 1){
            queryWrapper.ge(Score::getEtScore,60);
        }
        if (searchParam.getSix() == 2){
            queryWrapper.lt(Score::getEtScore,60);
        }
        return scoreMapper.selectPage(page,queryWrapper);
    }
}
